﻿using LoungeRepo.Normalizer.DataStructures;

namespace LoungeRepo.Normalizer
{
    partial class ObjectGraphNormalizer
    {
        private void TransferObjectIntoNormalizedObject(object source, NormalizedObject destination)
        {
            foreach (var field in source.AllFieldsOfObjectType())
                if (!field.IsNotSerialized)
                    destination.AddField(field.Name, NormalizeFieldValue(field.GetValue(source)));
        }


        internal object NormalizeFieldValue(object fieldValue)
        {
            object normalizedFieldValue;

            if (TryNormalizeArrayValue(fieldValue, out normalizedFieldValue))
                return normalizedFieldValue;

            if (TryNormalizeSimpleTypeValue(fieldValue, out normalizedFieldValue))
                return normalizedFieldValue;

            return NormalizeComplexTypeValue(fieldValue);
        }
    }
}
